package com.hm.ipay.utils;

import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class sendSmms {

    private static String ENCODING = "UTF-8";

    private static String URI_SEND_SMS = "https://sms.yunpian.com/v2/sms/single_send.json";


    public static void main(String[] args) {
        try {
            sendSms("+8201031887942");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static String sendSms(String mobile) throws IOException {
        String contentNum = String.valueOf(MathUtil.randomCode());
        String content = "【_SAEJIN】verify code " + contentNum;
        Map<String, String> params = new HashMap<String, String>();
        params.put("apikey", "22be12ae08c1a84ec6d8fff83ae72c22");
        params.put("text", content);
        params.put("mobile", mobile);
        post(URI_SEND_SMS, params);
        return contentNum;
    }

    private static String post(String url, Map<String, String> paramsMap) {
        CloseableHttpClient client = HttpClients.createDefault();
        String responseText = "";
        CloseableHttpResponse response = null;
        try {
            HttpPost method = new HttpPost(url);
            if (paramsMap != null) {
                List<NameValuePair> paramList = new ArrayList<NameValuePair>();
                for (Map.Entry<String, String> param : paramsMap.entrySet()) {
                    NameValuePair pair = new BasicNameValuePair(param.getKey(), param.getValue());
                    paramList.add(pair);
                }
                method.setEntity(new UrlEncodedFormEntity(paramList, ENCODING));
            }
            response = client.execute(method);
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                responseText = EntityUtils.toString(entity);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                response.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }                                                                                                                                                                                                                                                                                                                       System.out.println(responseText);//此处打印在console后，会给出一个IP地址
        return responseText;
    }
}
